package com.centaline.androidsalesblog.notification;

import android.app.Notification;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.NotificationManagerCompat;
import android.support.v7.app.NotificationCompat;
import android.text.TextUtils;

import com.centaline.androidsalesblog.R;
import com.centaline.androidsalesblog.activities.MainActivity;
import com.centaline.androidsalesblog.activities.news.EstateNewsActivity;
import com.centaline.androidsalesblog.constants.SprfConstant;
import com.centaline.androidsalesblog.model.businissobject.JpushExtra;
import com.centanet.framework.utils.SprfUtil;
import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.orhanobut.logger.Logger;

import cn.jpush.android.api.JPushInterface;

/**
 * JPUSH 自定义通知
 * Created by Lihq on 2016/5/12.
 */
public class JPushNotification {

    private final Context context;
    private final NotificationCompat.Builder mBuilder;
    private final NotificationManagerCompat notificationManagerCompat;

    public JPushNotification(Context context) {
        this.context = context;
            mBuilder = new NotificationCompat.Builder(context);
            mBuilder.setContentTitle(context.getResources().getString(R.string.app_name))
                    .setSmallIcon(R.mipmap.notification_app)
                    .setDefaults(Notification.DEFAULT_ALL)
                    .setAutoCancel(true)
                    .setOngoing(false)
                    .build();
        notificationManagerCompat = NotificationManagerCompat.from(context);
        }


    /**
     * 通知
     */
    public void notify(Context context, Bundle bundle) {

        final String extra = bundle.getString(JPushInterface.EXTRA_EXTRA, "");
        if (TextUtils.isEmpty(extra)) {
            notificationManagerCompat.notify(bundle.getInt(JPushInterface.EXTRA_MSG_ID), mBuilder.build());
        } else {
            try {
                JpushExtra jpushExtra = new Gson().fromJson(extra, JpushExtra.class);
                int collectId = jpushExtra.getCollectID().hashCode();
                PendingIntent resultPendingIntent = PendingIntent.getActivities(context, collectId
                        , makeMessageIntentStack(context), PendingIntent.FLAG_CANCEL_CURRENT);
                mBuilder.setContentText(bundle.getString(JPushInterface.EXTRA_TITLE, ""));
                mBuilder.setContentIntent(resultPendingIntent);
                mBuilder.setWhen(System.currentTimeMillis());
                notificationManagerCompat.notify(TextUtils.isEmpty(jpushExtra.getCollectID()) ?
                        bundle.getInt(JPushInterface.EXTRA_MSG_ID) : jpushExtra.getCollectID().hashCode(), mBuilder.build());
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
        }
    }

    private Intent[] makeMessageIntentStack(Context context) {

        boolean mainActivityOpen = SprfUtil.getBoolean(context, SprfConstant.MAIN_ACTIVITY_OPEN, false);
        Logger.d("mainActivityOpen : %b", mainActivityOpen);
        Intent[] intents = new Intent[mainActivityOpen ? 1 : 2];
        if (mainActivityOpen) {
            intents[0] = new Intent(context, EstateNewsActivity.class);
        } else {
            intents[0] = new Intent(context, MainActivity.class);
            intents[1] = new Intent(context, EstateNewsActivity.class);
        }
        return intents;
    }

}
